package com.dixiang.framework;

import java.util.Map;
import com.baidu.mobstat.StatService;
import com.dixiang.framework.utils.Utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public abstract class BaseActivity extends Activity {
	public Activity activity;
	private Map<String, BaseActivity> map = Utils.map;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = this;
		addTask(this);
		Utils.initExcption(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		initHeaderView();
		StatService.onResume(activity);
		// umeng
		// MobclickAgent.onResume(activity);//统计

	}

	@Override
	protected void onPause() {
		super.onPause();
		// umeng
		// MobclickAgent.onPause(activity);//统计
		StatService.onPause(activity);
	}

	public abstract void initHeaderView();

	public void initBackView(int resourseId, int stringId,
			OnClickListener onListener) {
		View backView = findViewById(R.id.back_view);
		ImageView img_back = (ImageView) findViewById(R.id.img_back);
		TextView txt_back = (TextView) findViewById(R.id.lab_back);

		if (resourseId != 0) {
			img_back.setBackgroundResource(resourseId);
		}
		if (stringId != 0) {
			txt_back.setText(stringId);
		}
		backView.setOnClickListener(onListener);
	}

	public void initBackView(OnClickListener onListener) {
		initBackView(0, 0, onListener);
	}

	public void initBackView(boolean isShow) {
		View backView = findViewById(R.id.back_view);
		if (!isShow) {
			backView.setVisibility(View.GONE);
		} else {
			backView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					finish();
				}
			});
		}
	}

	public void initOptionView(int resourseId, int stringId,
			OnClickListener onListener) {
		View optionView = findViewById(R.id.option_view);
		optionView.setVisibility(View.VISIBLE);
		ImageView img_option = (ImageView) findViewById(R.id.img_option);
		TextView txt_option = (TextView) findViewById(R.id.lab_option);

		if (resourseId != 0) {
			img_option.setBackgroundResource(resourseId);
		}
		if (stringId != 0) {
			txt_option.setText(stringId);
		}
		optionView.setOnClickListener(onListener);
	}

	public void setRightText(String rightText, int colorid) {
		TextView txt_option = (TextView) findViewById(R.id.lab_option);
		txt_option.setVisibility(View.VISIBLE);
		txt_option.setText(rightText);
		if (colorid != 0) {
			txt_option.setTextColor(getResources().getColor(colorid));
		}
	}

	public void initOptionView(OnClickListener onListener) {
		initOptionView(0, 0, onListener);
	}

	public void setTitleViewValue(int stringId) {
		setTitleViewValue(stringId, 0, 0);
	}

	public void setTitleViewValue(int stringId, int size, int colorid) {
		TextView title = (TextView) findViewById(R.id.lab_title);
		title.setVisibility(View.VISIBLE);
		if (size != 0) {
			title.setTextSize(size);
		}

		if (stringId != 0) {
			title.setText(stringId);
		}

		if (colorid != 0) {
			title.setTextColor(getResources().getColor(colorid));
		}
	}

	public void setTitleViewValue(String stringText) {
		TextView title = (TextView) findViewById(R.id.lab_title);
		title.setVisibility(View.VISIBLE);
		title.setText(stringText);
	}

	public void setTitleViewValue(String stringText, int colorid,
			int rightDrawable, OnClickListener onListener) {

		TextView title = (TextView) findViewById(R.id.lab_title);
		title.setVisibility(View.VISIBLE);
		title.setText(stringText);

		Drawable drawable = getResources().getDrawable(rightDrawable);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight()); // 设置边界

		title.setCompoundDrawables(null, null, drawable, null);// 画在右边
		title.setCompoundDrawablePadding(3);

		if (colorid != 0) {
			title.setTextColor(getResources().getColor(colorid));
		}
		if (onListener != null) {
			title.setOnClickListener(onListener);
		}

	}

	public void initCenterView(View view) {

		RelativeLayout sel_view = (RelativeLayout) findViewById(R.id.sel_view);
		sel_view.setVisibility(View.VISIBLE);
		sel_view.addView(view);

		TextView title = (TextView) findViewById(R.id.lab_title);
		title.setVisibility(View.GONE);
	}

	public void clearTitle() {
		View backView = activity.findViewById(R.id.back_view);
		backView.setVisibility(View.GONE);

		View sel = activity.findViewById(R.id.sel_view);
		sel.setVisibility(View.GONE);

		View optionView = activity.findViewById(R.id.option_view);
		optionView.setVisibility(View.GONE);
	}

	@SuppressLint("NewApi")
	public void setTopViewBg(int resourseId) {
		View topView = findViewById(R.id.actionbar);
		Drawable drawable = activity.getResources().getDrawable(resourseId);
		topView.setBackground(drawable);
	}
	
	public void setTitleOption(int stringId) {
		TextView txt_option = (TextView) findViewById(R.id.lab_option);
		if (stringId != 0) {
			txt_option.setText(stringId);
		}
	}
	
	private void addTask(BaseActivity activity) {
		map.put(activity.getClass().getSimpleName(), activity);
	}

	private void removeTask(BaseActivity activity) {
		map.remove(activity.getClass().getSimpleName());
	}

	public void closeActivity(String cls) {
		Activity activity = map.get(cls);
		map.remove(cls);
		if (activity != null)
			activity.finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Activity activity = map.get(getClass().getSimpleName());
		removeTask(this);
		if (activity != null)
			activity.finish();
	}
}
